package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.base.BaseModel;
import com.sphd.beautysalon.info.DuihuanInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.JifenCommodityPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
* Created by Hu on 2016/12/22
*/

public class JifenCommodityModelImpl implements JifenCommodityModel, VolleyJsonObjectCallBack {
    JifenCommodityPresenterImpl jifenCommodityPresenter;
    @Override
    public void sendUrl(String url, JifenCommodityPresenterImpl jifenCommodityPresenter) {
        this.jifenCommodityPresenter=jifenCommodityPresenter;
        BaseModel.volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void delUrl(String url, JifenCommodityPresenterImpl jifenCommodityPresenter) {
        BaseModel.volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res=response.getInt("res");
                    jifenCommodityPresenter.delRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            jifenCommodityPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<DuihuanInfo> duihuanInfos = JsonUtil.getListFromString(response.getString("res"),DuihuanInfo.class);
            jifenCommodityPresenter.recList(duihuanInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        jifenCommodityPresenter.sendError(error);
    }
}